package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.NoticeReadDao;
import com.framework.entity.flow.FlowNoticeTer;
import com.framework.entity.flow.FlowNoticeTerInfo;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysNotice;
import com.framework.entity.sys.SysNoticeRead;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.redis.util.RedisUtil;
import com.framework.service.AgentService;
import com.framework.service.DistribuService;
import com.framework.service.NoticeReadService;
import com.framework.view.FlowNoticeTerView;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("noticeReadService")
public class NoticeReadServiceImpl extends ServiceImpl<NoticeReadDao, SysNoticeRead> implements NoticeReadService {

	@Autowired
	private AgentService agentService;
	@Autowired
	private DistribuService distribuService;
	@Autowired
	private RedisUtil redisUtil;
	@Override
	public List<FlowNoticeTerView> getNoticeTerList(String token) {
		//缓存中代理商信息
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		return baseMapper.getNoticeTerList(agentId);
	}

	@Override
	public Map<String, Object> getNoticeTerInfo(String noticeTerNo) {
		Map<String,Object> map = new HashedMap();
		Map<String,Object> infoMap = baseMapper.getNoticeTerInfoMap(noticeTerNo);
		map.put("flowNoticeTer",infoMap);
		List<Map<String,Object>> list = baseMapper.getNoticeTerInfoList(noticeTerNo);
		map.put("terList",list);
		//已读操作
		baseMapper.isReadTer(noticeTerNo);
		return map;
	}

	@Override
	public Map<String, Object> getMessageNotice(String token) {
		//缓存中代理商信息
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		Long deptId = agent.getDeptId();
		Map<String,Object> map = new HashedMap();
		//分配通知数量
		int disCount = baseMapper.disCount(agentId);
		//计算未读系统通知数量
		int unreadSysCount = 0;
		List<Map<String,Object>> sysList = baseMapper.querySysNoticeList(agentId,deptId);
		for (int i = 0; i < sysList.size(); i++) {
			String isRead = (String)sysList.get(i).get("isRead");
			if(StringUtils.equals("否",isRead)){
				unreadSysCount++;
			}
		}
		//百易助手通知数量
		int assCount = baseMapper.assCount(agentId);
		map.put("disCount",disCount);
		map.put("sysCount",unreadSysCount);
		map.put("assCount",assCount);
		return map;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void acceptTer(String token,String noticeTerNo){
		if(StringUtils.isBlank(noticeTerNo)){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		//缓存中代理商信息
		//SysAgent agent = agentService.queryAgentByToken(token);
		//Long agentId = agent.getAgentId();
		FlowNoticeTer flowNoticeTer = baseMapper.getNoticeTerInfo(noticeTerNo);
		if(flowNoticeTer == null){
			throw new RRException("9999","未查询到所属通知消息");
		}
		if(flowNoticeTer.getIsAccept() == 2){
			throw new RRException("9999","终端已经接受成功");
		}
		//查询需要收货的机器列表
		List<Map<String,Object>> list = baseMapper.getNoticeTerInfoList(noticeTerNo);
		for (int i = 0; i < list.size(); i++) {
			String sn = (String)list.get(i).get("sn");
			//终端设置为签收成功
			distribuService.updateAccept(sn,flowNoticeTer.getAcceptAgentId());
		}
		//通知设置为接受成功
		baseMapper.acceptTer(noticeTerNo);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertNoticeTer(FlowNoticeTer flowNoticeTer) {
		if(flowNoticeTer == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		baseMapper.insertNoticeTer(flowNoticeTer);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void insertNoticeTerInfo(FlowNoticeTerInfo flowNoticeTerInfo) {
		if(flowNoticeTerInfo == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		baseMapper.insertNoticeTerInfo(flowNoticeTerInfo);
	}

	@Override
	public List<Map<String, Object>> querySysNoticeList(String token) {
		//缓存中代理商信息
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		Long deptId = agent.getDeptId();
		return baseMapper.queryNoticeList(agentId,deptId);
	}

	@Override
	public SysNotice querySysNoticeInfo(String token, String sysNoticeNo) {
		//缓存中代理商信息
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		Long deptId=agent.getDeptId();
		SysNotice sysNotice = baseMapper.querySysNoticeInfo(sysNoticeNo,deptId);
		if(sysNotice == null){
			throw new RRException("9999","未查询到系统通知信息");
		}
		//查询是否已读
		boolean isRead = baseMapper.sysNoticeIsRead(sysNoticeNo,agentId);
		if(isRead == false){
			//添加已读信息
			SysNoticeRead s = new SysNoticeRead();
			s.setAgentId(agentId);
			s.setNoticeNo(sysNoticeNo);
			s.setRedDateTime(new Date());
			baseMapper.insert(s);
		}
		return sysNotice;
	}

	@Override
	public boolean isAcceptTer(String token) {
		//缓存中代理商信息
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		return baseMapper.isAcceptTer(agentId);
	}

	@Override
	public List<Map<String, Object>> queryCarouselNoticeList(String token) {
		SysAgent agent = agentService.queryAgentByToken(token);
		return baseMapper.queryCarouselNoticeList(agent.getDeptId());
	}

	@Override
	public Map<String, Object> getMessageNoticeCount(String token) {
		//缓存中代理商信息
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		Long deptId = agent.getDeptId();
		Map<String,Object> map = new HashedMap(2);
		//计算未读系统通知数量
		int unreadSysCount = 0;
		List<Map<String,Object>> sysList = baseMapper.querySysNoticeList(agentId,deptId);
		for (int i = 0; i < sysList.size(); i++) {
			String isRead = (String)sysList.get(i).get("isRead");
			if(StringUtils.equals("否",isRead)){
				unreadSysCount++;
			}
		}
		//百易助手通知数量
		int assCount = baseMapper.assCount(agentId);
		map.put("sysCount",unreadSysCount);
		map.put("assCount",assCount);
		return map;
	}
}
